//
//  HJNewFeatureController.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/23.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"

class HJNewFeatureController: UICollectionViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
        
    }
    
    ///流水布局
    let layout = UICollectionViewFlowLayout()
    
    init() {
        
        super.init(collectionViewLayout:layout)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func prepareCollectionView(){
        
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
        //注册Cell
        collectionView?.registerClass(HJNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
}
///collectionview的数据源和代理方法
extension HJNewFeatureController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HJNewFeaturePageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)as!HJNewFeatureCell
        
        
        cell.index = indexPath.item
        
        return cell
        
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //计算当前页
        let page = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        
        if page == HJNewFeaturePageCount - 1{
            
            //创建indexPath
            let indexPath = NSIndexPath(forItem: page, inSection: 0)
            
            //获取indexPath对应的cell
            let cell = collectionView?.cellForItemAtIndexPath(indexPath)as! HJNewFeatureCell
            
            //按钮开始动画
            
            cell.startButtonAnimation()
            
        }
        
    }
    
}

///自定义cell

class HJNewFeatureCell: UICollectionViewCell{
    
    var index: Int = 0{
        
        didSet{
            
            bkgImageView.image = UIImage(named: "new_feature_\(index+1)")
            
            startButton.hidden = true
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startButtonAnimation(){
        startButton.hidden = false
        
        //开始动画之前 先将scale设置为0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }, completion: nil)
        
        
        
    }
    
    func prepareUI(){
        //添加子控件
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        //添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        //背景
        //        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //
        //        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //
        //        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //
        //
        //        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        bkgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView)
        }
        
        
        
        //按钮
        
        //        contentView.addConstraint(NSLayoutConstraint(item:startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //
        //        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        startButton.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
        
    }
    
    func didClickStartButton(){
        
        
        AppDelegate.OutSwitchRootViewController(HJTabBarController())
    }
    
    
    
    
    //MARK: 懒加载
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "didClickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    
    
    
}




